import { queryBillEdit, updateBillLock, queryOrgList } from '@/api/minix'
import { mapGetters } from 'vuex'
// 查询单据是否可操作
export default {
  data() {
    return {
      aotuTab: false,
      aotuTab1: false,
      fnumber1: '',
      teamList: [],
      isDisabled1: false
    }
  },
  created() {
    this.queryOrgit()
    if (this.$route.params.id) {
      // this.queryBillEdit()
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  computed: {
    ...mapGetters(['userName'])
  },
  methods: {
    getAtuo(index, fnumber, en) {
      this.outIn = en
      this.indexSelf = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber1 = fnumber
      }, 1000)
    },
    getAtuo1(index, fnumber, en) {
      this.outIn = en
      this.indexSelf = index
      this.aotuTab1 = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber1 = fnumber
      }, 1000)
    },
    // 销售组织
    queryOrgit() {
      queryOrgList({}).then(res => {
        this.teamList = res.data.list
      })
    },
    queryBillEdit() {
      const DATA = {
        fbillNo: this.$route.params.id,
        fuserName: this.userName,
        targerName: this.targerName
      }
      queryBillEdit(DATA).then(res => {
        if (res.code === 0) {
          this.update()
        } else {
          this.isDisabled1 = true
          setTimeout(() => {
            this.isDisabled = true
          }, 500)
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
        }
      })
    },
    update() {
      var vm = this
      clearInterval(vm.timer)
      vm.timer = setInterval(this.billLock, 10000)
    },
    billLock() {
      const DATA = {
        fbillNo: this.$route.params.id,
        fuserName: this.userName,
        targerName: this.targerName
      }
      updateBillLock(DATA).then(res => {
        if (res.code !== 0) {
          clearInterval(this.timer)
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 自定义合计行
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return (Number(prev) + Number(curr)).toFixed(3)
            } else {
              return prev
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      })
      sums.forEach((res, index) => {
        sums[index] = Number(sums[index]).toLocaleString()
        if (sums[index] <= 0) {
          sums[index] = ''
        }
      })
      sums[0] = '合计'
      return sums
    }
  }
}
